/*
 * test.cpp
 *
 *  Created on: 2021年2月21日
 *      Author: kimfeng
 */

#include "server_socket.h"

static server_socket *pg_server = NULL;
static const std::size_t thd_num = 4;

static int echo_func(const sck_pkg_t *pkg, vec_data_t& rsp)
{
	std::size_t n = pkg->len + 1;
	char *message = (char *)malloc(n);
	if (NULL == message)
	{
		return ENOMEM;
	}
	memset(message, 0, n);
	memcpy(message, pkg->payload, pkg->len);
	log_info("message size %u \"%s\"", pkg->len, message);

	for (std::size_t i=0; i<pkg->len; i++)
		rsp.push_back(message[i]);
	return 0;
}

int main(int argc, char **argv)
{
	logger_init(eTrace, true);

	pg_server = new server_socket("0.0.0.0", "6000", thd_num, true);
	if (NULL == pg_server)
	{
		return ENOMEM;
	}

	pg_server->attech_req_func(0xDF01, echo_func);
	pg_server->run();

	free(pg_server);
	return 0;
}
